package com.xhtt.modules.cfg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.common.exception.RRException;
import com.xhtt.modules.cfg.dao.CfgIslandMapper;
import com.xhtt.modules.cfg.entity.CfgIsland;
import com.xhtt.modules.cfg.entity.CfgStand;
import com.xhtt.modules.cfg.service.CfgIslandService;
import com.xhtt.modules.cfg.service.CfgStandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author feipc
 * @since 2019-02-18
 */
@Service
public class CfgIslandServiceImpl extends ServiceImpl<CfgIslandMapper, CfgIsland> implements CfgIslandService {
    @Autowired
    private CfgStandService cfgStandService;

    @Override
    public List<CfgIsland> islandList(Map<String, Object> params) {
        Object name = params.get("name");
        Object loadAreaType = params.get("loadAreaType");
        List<CfgIsland> list = this.list(new LambdaQueryWrapper<CfgIsland>().eq(CfgIsland::getStatus, 1).like(!ObjectUtils.isEmpty(name), CfgIsland::getName, name).eq(!ObjectUtils.isEmpty(loadAreaType), CfgIsland::getLoadAreaType, loadAreaType));
        return list;
    }

    @Override
    public void addIsland(CfgIsland cfgIsland) {
        this.save(cfgIsland);
    }

    @Override
    public void deleteIsland(Integer id) {
        int count = cfgStandService.count(new LambdaQueryWrapper<CfgStand>().eq(CfgStand::getIslandId, id).eq(CfgStand::getStatus, 1));
        if (count > 0) {
            throw new RRException("请先删除装车台信息");
        }
        this.update(new CfgIsland(), new UpdateWrapper<CfgIsland>().set(CfgIsland.STATUS, -1).set(CfgIsland.USERID, null).eq(CfgIsland.ID, id));
    }

    @Override
    public void removeUser(Integer userId) {
        baseMapper.removeUser(userId);
    }

    @Override
    public List<CfgIsland> loadAreaList() {
        List<CfgIsland> list = baseMapper.loadAreaList();
        return list;
    }
}
